<template>
  <div>
    <!--面包屑导航-->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--card卡片-->
    <el-card>
      <!--搜索-->
      <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @close="clearQuery" style="width: 300px">
        <el-button slot="append" icon="el-icon-search" @click="searchQuery"></el-button>
      </el-input>
      <!--table表格-->
      <el-table :data="tableData" style="width: 100%" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="300px"></el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column label="是否付款">
          <template slot-scope="scope">
            <el-tag type="danger" size="small" v-if="scope.row.order_pay === '0'">未付款</el-tag>
            <el-tag type="success" size="small" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column label="下单时间" width="180px">
          <template slot-scope="scope">
            {{scope.row.create_time | myDate}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="addressDialog=true"></el-button>
          <el-button size="mini" type="success" icon="el-icon-location" @click="showProgress"></el-button>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!---->
    <!---->
    <!---->
    <!--修改地址对话框-->
    <el-dialog
      @close="closeAddressDialog"
      title="修改地址"
      :visible.sync="addressDialog"
      width="40%">
      <el-form :model="addressForm" :rules="addressRules" ref="addressRef" label-width="100px">
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            v-model="addressForm.address1"
            :options="cityData"
            :props="{ expandTrigger: 'hover' }"
            @change="addressChange">
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2" style="width: 100%"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressDialog = false">取 消</el-button>
        <el-button type="primary" @click="addressDialog = false">确 定</el-button>
      </span>
    </el-dialog>
    <!---->
    <!---->
    <!---->
    <!--查看物流信息-->
    <el-dialog
      @close="closeProgress"
      title="查看物流信息"
      :visible.sync="progressDialog"
      width="50%">
      <el-timeline reverse>
        <el-timeline-item
          v-for="(item, index) in progressDate"
          :key="index"
          :timestamp="item.time">
          {{item.context}}
        </el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="progressDialog = false">取 消</el-button>
        <el-button type="primary" @click="progressDialog = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import cityData from './citydata'
  export default {
    data () {
      return {
        // 查询订单参数
        queryInfo: {
          query: '',
          pagenum: 1,
          pagesize: 10
        },
        tableData: [],
        total: 0,
        // 修改地址的对话框
        addressDialog: false,
        addressForm: {
          address1: [],
          address2: ''
        },
        addressRules: {
          address1: [
            { required: true, message: '请选择省市区/县', trigger: 'blur' }
          ],
          address2: [
            { required: true, message: '请输入详细地址', trigger: 'blur' }
          ]
        },
        cityData: cityData,
        // 查看物流信息
        progressDialog: false,
        progressDate: []
      }
    },
    created () {
      this.getOrders()
    },
    methods: {
      // 获取订单数量
      getOrders: async function () {
        const { data: res } = await this.$http.get('orders', {
          params: this.queryInfo
        })
        if (res.meta.status !== 200) return this.$message.error('获取订单数据失败')
        this.tableData = res.data.goods
        this.total = res.data.total
      },
      // 每页条数
      handleSizeChange(val) {
        this.queryInfo.pagesize = val
        this.getOrders()
      },
      // 当前第几页
      handleCurrentChange(val) {
        this.queryInfo.pagenum = val
        this.getOrders()
      },
      // 点击搜索
      searchQuery: function () {
        console.log(this.queryInfo)
        this.getOrders()
      },
      // 清空搜索
      clearQuery: function () {
        this.getOrders()
      },
      // 修改地址
      addressChange(value) {
        console.log(value)
      },
      // 管理address的对话框，清空数据
      closeAddressDialog: function () {
        this.$refs.addressRef.resetFields()
      },
      // 查看物流信息
      showProgress: async function () {
        this.progressDialog = true
        const { data: res } = await this.$http.get('/kuaidi/1106975712662')
        if (res.meta.status !== 200) return this.$message.error('获取物流信息失败')
        this.progressDate = res.data
      },
      closeProgress: function () {
        this.progressDate = []
      }
    }
  }
</script>

<style lang="less" scoped>
  .el-cascader{
    width: 100%;
  }
</style>
